package com.beiye.drivertransport.thematic.learning;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.RadioButton;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.beiye.drivertransport.R;
import com.beiye.drivertransport.activity.TwoBaseAty;
import com.beiye.drivertransport.bean.CheckBean;
import com.beiye.drivertransport.bean.CheckExamtimeBean;
import com.beiye.drivertransport.bean.LoginUserBean;
import com.beiye.drivertransport.http.Login;
import com.beiye.drivertransport.utils.TiShiDialog;
import com.beiye.drivertransport.utils.UserManger;
import com.beiye.drivertransport.view.LiumRadioGroup;
import com.githang.statusbar.StatusBarCompat;


import butterknife.OnClick;
import okhttp3.Call;
import okhttp3.Response;

//管理员考试前信息补全
public class AdministratorInformationcompletionActivity extends TwoBaseAty {

    ImageView img_improveback;

    EditText ed_admin;

    EditText ed_admin1;

    EditText ed_admin2;

    EditText ed_admin3;

    TextView tv_imput;

    LiumRadioGroup admin_rg;

    RadioButton radioButton_admin;

    RadioButton radioButton_admin1;
    private PopupWindow mUserPopWindow;
    private int stId;
    private String orgId;

    @Override
    public int getLayoutId() {
        return R.layout.activity_administrator_informationcompletion;

    }

    public void onClick(View view) {
        int id = view.getId();
        if (id == R.id.img_improveback) {
            finish();
        } else if (id == R.id.tv_imput) {
            String namestr = ed_admin3.getText().toString().trim();
            if (namestr.equals("")) {
                showToast("请您输入学员姓名，才可以保存");
                return;
            }
            String companystr = ed_admin.getText().toString().trim();
            if (companystr.equals("")) {
                showToast("请您输入工作单位，才可以保存");
                return;
            }

            SharedPreferences sp = getSharedPreferences("AdministratorInformationcompletionActivity", Context.MODE_PRIVATE);
            String adminstr = sp.getString("admin", "");
            if (adminstr.equals("")) {
                showToast("请您选中工作岗位，才可以保存");
                return;
            }
            String IDstr = ed_admin1.getText().toString().trim();
            if (IDstr.equals("")) {
                showToast("请您输入身份证，才可以保存");
                return;
            }
            String addressstr = ed_admin2.getText().toString().trim();
            if (addressstr.equals("")) {
                showToast("请您输入通讯地址，才可以保存");
                return;
            }
            //复核信息
            showpopwindow();
        }
    }
    private void showpopwindow() {
        //设置contentView
        View contentView = LayoutInflater.from(AdministratorInformationcompletionActivity.this).inflate(R.layout.admin_popwindowlayout, null);
        mUserPopWindow = new PopupWindow(contentView,
                WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.MATCH_PARENT, true);

        final LinearLayout le_takepicture = (LinearLayout)contentView.findViewById(R.id.le_takepicture);//跟布局
        TextView img_sure =(TextView) contentView.findViewById(R.id.img_photo1);//确定
        TextView img_no =(TextView) contentView.findViewById(R.id.img_photo2);//取消
        TextView tv_company = (TextView) contentView.findViewById(R.id.tv_photo8);
        TextView tv_use = (TextView) contentView.findViewById(R.id.tv_photo9);
        TextView tv_id = (TextView) contentView.findViewById(R.id.tv_photo10);
        TextView tv_adress = (TextView) contentView.findViewById(R.id.tv_photo11);
        TextView tv_name = (TextView) contentView.findViewById(R.id.tv_photo12);
       final String namestr = ed_admin3.getText().toString().trim();
        tv_name.setText("学员姓名："+namestr);
       final   String companystr = ed_admin.getText().toString().trim();
        tv_company.setText("工作单位："+companystr);
        SharedPreferences sp = getSharedPreferences("AdministratorInformationcompletionActivity", Context.MODE_PRIVATE);
       final  String adminstr = sp.getString("admin", "");
        tv_use.setText("工作岗位："+adminstr);
       final  String IDstr = ed_admin1.getText().toString().trim();
        tv_id.setText("身份证号："+IDstr);
       final String addressstr = ed_admin2.getText().toString().trim();
        tv_adress.setText("通讯地址："+addressstr);
        mUserPopWindow.setFocusable(false);
        mUserPopWindow.setOutsideTouchable(false);
        mUserPopWindow.showAtLocation(le_takepicture, Gravity.CENTER, 0, 0);

        img_sure.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mUserPopWindow.dismiss();
                LoginUserBean userInfo = UserManger.getUserInfo();
                LoginUserBean.DataBean data = userInfo.getData();
                String userId = data.getUserId();
                initAdminstratorSave(userId,companystr,namestr,IDstr,addressstr,adminstr);//提交信息

            }
        });
        img_no.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mUserPopWindow.dismiss();

            }
        });

    }

    private void initAdminstratorSave(String userId, String companystr, String namestr, String IDstr, String addressstr, String adminstr) {
        showLoadingDialog("");
        new Login().getsaveuserStCert(userId, orgId,stId,companystr,namestr,IDstr,addressstr,adminstr,this, 2);
    }

    @Override
    public void initData() {
        img_improveback = (ImageView) findViewById(R.id.img_improveback);
        ed_admin = (EditText) findViewById(R.id.ed_admin);
        ed_admin1 = (EditText) findViewById(R.id.ed_admin1);
        ed_admin2 = (EditText) findViewById(R.id.ed_admin2);
        ed_admin3 = (EditText) findViewById(R.id.ed_admin3);
        tv_imput = (TextView) findViewById(R.id.tv_imput);
        admin_rg = (LiumRadioGroup) findViewById(R.id.admin_rg);
        radioButton_admin = (RadioButton) findViewById(R.id.radioButton_admin);
        radioButton_admin1 = (RadioButton) findViewById(R.id.radioButton_admin1);

        StatusBarCompat.setStatusBarColor(this, Color.parseColor("#1F80C4"));

        admin_rg.setOnCheckedChangeListener(new LiumRadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(LiumRadioGroup group, int checkedId) {
                if (checkedId == R.id.radioButton_admin) {
                    SharedPreferences sp = getSharedPreferences("AdministratorInformationcompletionActivity", Context.MODE_PRIVATE);
                    SharedPreferences.Editor edit = sp.edit();
                    edit.putString("admin", "企业负责人");
                    edit.commit();
                } else if (checkedId == R.id.radioButton_admin1) {
                    SharedPreferences sp1 = getSharedPreferences("AdministratorInformationcompletionActivity", Context.MODE_PRIVATE);
                    SharedPreferences.Editor edit1 = sp1.edit();
                    edit1.putString("admin", "安全管理员");
                    edit1.commit();
                }
            }
        });



        ed_admin.setOnTouchListener(new View.OnTouchListener() {

            @SuppressLint("ClickableViewAccessibility")
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (MotionEvent.ACTION_DOWN == event.getAction()) {
                    ed_admin.setCursorVisible(true);// 再次点击显示光标
                }
                return false;
            }
        });
        ed_admin1.setOnTouchListener(new View.OnTouchListener() {

            @SuppressLint("ClickableViewAccessibility")
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (MotionEvent.ACTION_DOWN == event.getAction()) {
                    ed_admin1.setCursorVisible(true);// 再次点击显示光标
                }
                return false;
            }
        });
        ed_admin2.setOnTouchListener(new View.OnTouchListener() {

            @SuppressLint("ClickableViewAccessibility")
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (MotionEvent.ACTION_DOWN == event.getAction()) {
                    ed_admin2.setCursorVisible(true);// 再次点击显示光标
                }
                return false;
            }
        });
        ed_admin3.setOnTouchListener(new View.OnTouchListener() {

            @SuppressLint("ClickableViewAccessibility")
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (MotionEvent.ACTION_DOWN == event.getAction()) {
                    ed_admin3.setCursorVisible(true);// 再次点击显示光标
                }
                return false;
            }
        });
        LoginUserBean userInfo = UserManger.getUserInfo();
        LoginUserBean.DataBean data = userInfo.getData();
        String userName = data.getUserName();
        ed_admin3.setText(userName);
    }

    @Override
    protected void onDestroy() {
        SharedPreferences sp = getSharedPreferences("AdministratorInformationcompletionActivity", Context.MODE_PRIVATE);
        SharedPreferences.Editor edit = sp.edit();
        edit.clear();
        edit.commit();
        super.onDestroy();
    }

    @Override
    public void requestData() {
        Bundle bundle = getIntent().getExtras();
        orgId = bundle.getString("orgId");
        stId = bundle.getInt("stId");
        LoginUserBean userInfo = UserManger.getUserInfo();
        LoginUserBean.DataBean data = userInfo.getData();
        String userId = data.getUserId();
        showLoadingDialog("");
        new Login().getfindUserByUserIdOrgId(userId, orgId,this, 1);
    }

    @Override
    public void onSuccess(String result, Call call, Response response, int what) {
        if(what==1)
        {
            dismissLoadingDialog();
            CheckExamtimeBean checkExamtimeBean = JSON.parseObject(result, CheckExamtimeBean.class);
            CheckExamtimeBean.DataBean dataBean = checkExamtimeBean.getData();
            String orgName = dataBean.getOrgName();
            String userName = dataBean.getUserName();
         //   String userMobile = dataBean.getUserMobile();
            String idcNo = dataBean.getIdcNo();
            String address = dataBean.getAddress();
             if(userName==null)
             {
                 ed_admin3.setText("");
             }else{
                 ed_admin3.setText(userName);
             }
            if(orgName==null)
            {
                ed_admin.setText("");
            }else{
                ed_admin.setText(orgName);
            }
            if(idcNo==null)
            {
                ed_admin1.setText("");
            }else{
                ed_admin1.setText(idcNo);
            }
            if(address==null)
            {
                ed_admin2.setText("");
            }else{
                ed_admin2.setText(address);
            }

        }else if(what==2)
        {
            dismissLoadingDialog();
            showToast("保存成功");
            Intent intent = new Intent();
            setResult(1, intent);
            finish();
        }
        super.onSuccess(result, call, response, what);
    }

    @Override
    public void onError(String result, Call call, Response response, int what) {
        if(what==2)
        {
            dismissLoadingDialog();
            CheckBean checkBean = JSON.parseObject(result, CheckBean.class);
            String msg = checkBean.getMsg();
            TiShiDialog.Builder builder1 = new TiShiDialog.Builder(AdministratorInformationcompletionActivity.this);
            builder1.setMessage(msg);
            builder1.setTitle("提示:");
            builder1.setPositiveButton("我知道", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                    dialog.dismiss();;
                    //设置你的操作事项
                }
            });
            builder1.create().show();
        }
        super.onError(result, call, response, what);
    }
}
